import java.util.Optional;

/**
 * 一般用OrElseGet
 */
public class OrElseGet {
    public static void main(String[] args) {
        // Student student = createStudent();
        Student student = null;
        Optional<Student> stuOptional = Optional.ofNullable(student);
        String name = stuOptional.map(Student::getName).orElseThrow(()-> new RuntimeException("name"));
        System.out.println(name);
        // Student stu1 = stuOptional.orElse(createStudent());
        // System.out.println(stu1);
        // Student stu2 = stuOptional.orElseGet(() -> createStudent());
        // System.out.println(stu2);
        // Student stu3 = stuOptional.orElseThrow(()-> new RuntimeException("student is null"));
        // System.out.println(stu3);
    }

    private static Student createStudent() {
        System.out.println("create student");
        return new Student(1, "xxt", 20);
    }
}
